import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_picker_plus/flutter_picker_plus.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flutter_base/config/app_color.dart';
import 'package:flutter_base/utils/extensions/get_interface_ext.dart';

typedef ArrayCallBack = void Function(dynamic selectValue, int selectIndex);
typedef DateCallBack = void Function(DateTime selectValue);

class PickerUtil {
  static bool _isShowPicker = false;

  /// 多列
  /// 多列选择器返回选中行对象数组和index数组
  static void showArrayPicker<T>(
    BuildContext context, {
    required List<String> data,
    String? title,
    String? labelKey, // 对象数组的文字字段
    int? selectIndex,
        ArrayCallBack? clickCallBack,
  }) {
    if (_isShowPicker || data.isEmpty) {
      return;
    }
    _isShowPicker = true;

    Picker picker = Picker(
        adapter: PickerDataAdapter<String>(
          pickerData: [data],
          isArray: true,
        ),
        itemExtent: 46.w,
        height: min(5, data.length)*46.w,
        backgroundColor: Colors.white,
        selectedTextStyle: TextStyle(color: AppColor.primary, fontSize: 16.sp, fontWeight: FontWeight.w500),
        title: SizedBox(height: 70.w, child: Text(title ?? "", style: TextStyle(fontWeight: FontWeight.w500, fontSize: 16.sp, color: Colors.black))),
        headerDecoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.vertical(top: Radius.circular(16.w))),
        cancelText: "取消",
        cancelTextStyle: TextStyle(fontWeight: FontWeight.w500, fontSize: 16.sp, color: Colors.black),
        confirmText: "确认",
        confirmTextStyle: TextStyle(fontWeight: FontWeight.w500, fontSize: 16.sp, color: Colors.black),
        selecteds: selectIndex != null ? [selectIndex] : null,
        onConfirm: (Picker picker, List value) {
          print(value.toString());
          print(picker.getSelectedValues());
          clickCallBack?.call(picker.getSelectedValues().first, value.first);
        });
    Get.bottomSheetX(
      picker.makePicker(),
    ).then((value) => _isShowPicker = false);
  }

  /// 日期选择器
  /// 时间选择器返回选中行时间（时间格式：2022-07-03 15:00:46）和index数组
  static void showDatePicker(
    BuildContext context, {
    String? title,
    DateTime? selectTime,
    int yearBegin = 1900,
    int yearEnd = 2100,
        DateCallBack? clickCallBack,
  }) {
    if (_isShowPicker) {
      return;
    }
    _isShowPicker = true;

    Picker picker = Picker(
        adapter: DateTimePickerAdapter(
          type: PickerDateTimeType.kYMD,
          isNumberMonth: true,
          monthSuffix: "月",
          daySuffix: "日",
          minValue: DateTime(yearBegin),
          maxValue: DateTime.now(),
          value: selectTime,
        ),
        changeToFirst: true,
        textAlign: TextAlign.left,
        itemExtent: 46.w,
        height: 230.w,
        backgroundColor: Colors.white,
        selectedTextStyle: TextStyle(color: AppColor.primary, fontSize: 16.sp, fontWeight: FontWeight.w500),
        title: SizedBox(height: 70.w, child: Text(title ?? "", style: TextStyle(fontWeight: FontWeight.w500, fontSize: 16.sp, color: Colors.black))),
        headerDecoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.vertical(top: Radius.circular(16.w))),
        cancelText: "取消",
        cancelTextStyle: TextStyle(fontWeight: FontWeight.w500, fontSize: 16.sp, color: Colors.black),
        confirmText: "确认",
        confirmTextStyle: TextStyle(fontWeight: FontWeight.w500, fontSize: 16.sp, color: Colors.black),
        onConfirm: (Picker picker, List value) {
          clickCallBack?.call(DateTime.parse(picker.adapter.text.split('.')[0]));
        });
    Get.bottomSheetX(
      picker.makePicker(),
    ).then((value) => _isShowPicker = false);
  }
}
